package ywpt.web.action.client;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientKeshi;
import ywpt.service.client.ClientKeshiService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientKeshiAction extends BaseAction implements
ModelDriven<ClientKeshi>, Preparable{

	/**
	 * 客户科室Action
	 */
	private static final long serialVersionUID = -5241162316806661339L;
	private Long id;
	private Long clientId;
	private ClientKeshi clientKeshi;
	private ClientKeshiService  clientKeshiService;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}
	
	public ClientKeshi getClientKeshi() {
		return clientKeshi;
	}

	public void setClientKeshi(ClientKeshi clientKeshi) {
		this.clientKeshi = clientKeshi;
	}

	public ClientKeshiService getClientKeshiService() {
		return clientKeshiService;
	}

	public void setClientKeshiService(ClientKeshiService clientKeshiService) {
		this.clientKeshiService = clientKeshiService;
	}

	@Override
	public ClientKeshi getModel() {
		return this.clientKeshi;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.clientKeshi = this.clientKeshiService.getClientKeshiById(id);
		}else{
			this.clientKeshi = new ClientKeshi();
		}
	}
	
	
	public String input(){	
		String code = this.clientKeshiService.CreateCode(clientId);
		clientKeshi.setCode(code);
		return INPUT;
	} 

	public String edit(){
		this.clientKeshi = clientKeshiService.getClientKeshiById(id);
		if(clientKeshi.getClient() != null){
			this.setClientId(clientKeshi.getClient().getId());
		}
		return INPUT;
	}
	/**
	 * ����
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				clientKeshiService.saveClientKeshi(clientKeshi ,clientId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} else {
				clientKeshiService.updateClientKeshi(clientKeshi,clientId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	 public String delete() throws Exception {
		if (id != null) 
		{	
			clientKeshiService.removeClientKeshi(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}


}
